﻿using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.StateMachine.Engine;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.StateMachine.SPI;

namespace T.FAS.Runtime.StateMachine.Service
{
    public class StateMachineService
    {
        /// <summary>
        /// 获取系统预制的动作
        /// </summary>
        /// <returns></returns>
        public List<IActionProcess> GetSysDefaultActions()
        {
            return ActionController.Current.GetSysDefaultActions();
        }

        /// <summary>
        /// 获取系统预制的规则
        /// </summary>
        /// <returns></returns>
        public List<IRuleProcess> GetSysDefaultRules()
        {
            return RuleController.Current.GetSysDefaultRules();
        }

        /// <summary>
        /// 执行事件
        /// </summary>
        /// <param name="eventKey">事件标识</param>
        /// <param name="record">CDO结果对象</param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public static TRecord FireStateChanging(StateMachineMetadata metadata, string eventKey, TRecord record, Dictionary<string, object> extParams = null)
        {
            return Engine.StateMachine.FireStateChanging(metadata, eventKey, record, extParams);
        }

        /// <summary>
        /// 执行事件
        /// </summary>
        /// <param name="eventKey">事件标识</param>
        /// <param name="record">CDO结果对象</param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public static TRecord FireBeforeSave(StateMachineMetadata metadata, string eventKey, TRecord record, Dictionary<string, object> extParams = null)
        {
            return Engine.StateMachine.FireBeforeSave(metadata, eventKey, record, extParams);
        }

        /// <summary>
        /// 执行事件
        /// </summary>
        /// <param name="eventKey">事件标识</param>
        /// <param name="record">CDO结果对象</param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public static TRecord FireAfterSave(StateMachineMetadata metadata, string eventKey, TRecord record, Dictionary<string, object> extParams = null)
        {
            return Engine.StateMachine.FireAfterSave(metadata, eventKey, record, extParams);
        }


        /// <summary>
        /// 校验数据在当前状态下的合法性
        /// </summary>
        /// <param name="record"></param>
        /// <returns></returns>
        public static ValidResult IsValid(StateMachineMetadata metadata, TRecord record)
        {
            return Engine.StateMachine.IsValid(metadata, record);
        }

        /// <summary>
        /// 是否可以触发事件
        /// </summary>
        /// <param name="eventKey">事件标识</param>
        /// <param name="record">CDO记录信息</param>
        /// <returns></returns>
        public static bool CanFire(StateMachineMetadata metadata, string eventKey, TRecord record)
        {
            return Engine.StateMachine.CanFire(metadata, eventKey, record, out object state);
        }

        /// <summary>
        /// 获取下一个可用状态状态信息
        /// </summary>
        /// <param name="metadata"></param>
        /// <returns></returns>
        public static List<TState> GetTransitionStates(StateMachineMetadata metadata, object stateKey, TRecord record = null)
        {
            return Engine.StateMachine.GetTransitionStates(metadata, stateKey, record);
        }

        /// <summary>
        /// 获取制定事件的详细信息
        /// </summary>
        /// <param name="metadata"></param>
        /// <param name="eventKey"></param>
        /// <returns></returns>
        public static TTransition GetTransition(StateMachineMetadata metadata, string eventKey)
        {
            return Engine.StateMachine.GetTransition(metadata, eventKey);
        }
    }
}
